package com.asktgapp.user.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.asktgapp.JGTool.Utils;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/2/2
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class EditNickFragment extends BaseFragment {

    private EditText mNick;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_edit_nick, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        setHasOptionsMenu(true);
        mNick = rootView.findViewById(R.id.et_nick);
        String nick = getActivity().getIntent().getStringExtra("nick");
        if(nick.length()>16){
            nick = nick.substring(0,17);
        }
        mNick.setText(nick);
        mNick.setSelection(nick.length());
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.finish, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (!Util.isEmpty(mNick.getText().toString().trim())) {
            Apiservice apiservice = ApiUtil.getInstance().create();
            HashMap params = new HashMap();
            params.put("username", mNick.getText().toString().trim());
            if (!Utils.noContainsEmoji(mNick.getText().toString().trim())) {
                showTost("不支持输入emoji表情", 1);
                return super.onOptionsItemSelected(item);
            }
            params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
            showProgress(getClass().getSimpleName());
            Call<ApiResponseBody> call = apiservice.updateNick(params);
            call.enqueue(new Callback<ApiResponseBody>() {
                @Override
                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                    dismissProgress();
                    if (response.isSuccessful()) {
                        Intent intent = new Intent();
                        intent.putExtra("nick", mNick.getText().toString().trim());
                        getActivity().setResult(Activity.RESULT_OK, intent);
                        getActivity().finish();
                    } else {
                        showTost(response.raw().message(), 0);
                    }
                }

                @Override
                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                    dismissProgress();
                    ApiException apiException = ExceptionHandle.handleException(t);
                    if (apiException.isTokenInvalid()) {
                        showUnLoginSnackbar();
                    } else if (apiException.isNetConnected()) {
                        showSetNetworkSnackbar();
                    } else {
                        showTost(apiException.getMessage(), 1);
                    }
                }
            });
        } else {
            showTost("请输入昵称", 0);
        }
        return super.onOptionsItemSelected(item);
    }
}
